package com.tfswx.jcyw.sqlmodel.api;

import com.tfswx.jcyw.sqlmodel.api.executor.Executor;
import com.tfswx.jcyw.sqlmodel.api.executor.SqlModelExecutor;
import com.tfswx.jcyw.sqlmodel.api.function.RuntimeFuntion;

import java.util.List;

/**
 * @author 张述江
 * @date 2023/9/7
 */
public interface SqlModelFactory {

    Executor createExecutor();

    List<RuntimeFuntion> createFunctionList();

    List<Operator> createOperatorList();

    static SqlModelFactory getSqlModelFactory(Class<? extends SqlModelFactory> sqlModelFactoryClass) {
        try {
            return sqlModelFactoryClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    default SqlModelExecutor createSqlModelExecutor() {
        SqlModelExecutor sqlModelExecutor = new SqlModelExecutor();
        Executor executor = createExecutor();
        sqlModelExecutor.setExecutor(executor);
        OperatorChain operatorChain = new OperatorChain();
        operatorChain.setFunctionList(createFunctionList());
        operatorChain.setOperatorList(createOperatorList());
        sqlModelExecutor.setOperatorChain(operatorChain);
        return sqlModelExecutor;
    }
}
